package vip.xiaonuo.biz.modular.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import vip.xiaonuo.biz.modular.user.entity.BizFzUser;
import vip.xiaonuo.biz.modular.user.param.BizFzUserAddParam;
import vip.xiaonuo.biz.modular.user.param.BizFzUserEditParam;
import vip.xiaonuo.biz.modular.user.param.BizFzUserIdParam;
import vip.xiaonuo.biz.modular.user.param.BizFzUserPageParam;
import vip.xiaonuo.biz.modular.user.param.BizFzUserRegisterParam;
import vip.xiaonuo.biz.modular.user.param.BizFzUserWxBindParam;
import vip.xiaonuo.biz.modular.user.param.BizFzUserWxLoginParam;
import vip.xiaonuo.biz.modular.user.service.BizFzUserService;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.common.pojo.CommonValidList;
import vip.xiaonuo.common.util.CommonCryptogramUtil;
import cn.hutool.core.util.ObjectUtil;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 分账用户表控制器
 *
 * @author gtc
 * 
 **/
@Api(tags = "分账用户表控制器")
@RestController
public class BizFzUserController {

    @Resource
    private BizFzUserService bizFzUserService;

    /**
     * 获取分账用户表分页
     *
     * @author gtc
     * 
     */
    @ApiOperation(value = "获取分账用户表分页", position = 1)
    @GetMapping("/biz/fzUser/page")
    public CommonResult<Page<BizFzUser>> page(BizFzUserPageParam bizFzUserPageParam) {
        return CommonResult.data(bizFzUserService.page(bizFzUserPageParam));
    }

    /**
     * 添加分账用户表
     *
     * @author gtc
     * 
     */
    @ApiOperation(value = "添加分账用户表", position = 2)
    @PostMapping("/biz/fzUser/add")
    public CommonResult<String> add(@RequestBody @Validated BizFzUserAddParam bizFzUserAddParam) {
        bizFzUserService.add(bizFzUserAddParam);
        return CommonResult.ok();
    }

    /**
     * 编辑分账用户表
     *
     * @author gtc
     * 
     */
    @ApiOperation(value = "编辑分账用户表", position = 3)
    @PostMapping("/biz/fzUser/edit")
    public CommonResult<String> edit(@RequestBody @Validated BizFzUserEditParam bizFzUserEditParam) {
        bizFzUserService.edit(bizFzUserEditParam);
        return CommonResult.ok();
    }

    /**
     * 删除分账用户表
     *
     * @author gtc
     * 
     */
    @ApiOperation(value = "删除分账用户表", position = 4)
    @PostMapping("/biz/fzUser/delete")
    public CommonResult<String> delete(@RequestBody @Validated CommonValidList<BizFzUserIdParam> bizFzUserIdParamList) {
        bizFzUserService.delete(bizFzUserIdParamList);
        return CommonResult.ok();
    }

    /**
     * 获取分账用户表详情
     *
     * @author gtc
     * 
     */
    @ApiOperation(value = "获取分账用户表详情", position = 5)
    @GetMapping("/biz/fzUser/detail")
    public CommonResult<BizFzUser> detail(BizFzUserIdParam bizFzUserIdParam) {
        return CommonResult.data(bizFzUserService.detail(bizFzUserIdParam));
    }

    /**
     * 绑定微信小程序openid
     *
     * @author gtc
     * 
     */
    @ApiOperation(value = "绑定微信小程序openid", position = 6)
    @PostMapping("/biz/fzUser/bindWxOpenId")
    public CommonResult<Boolean> bindWxOpenId(@RequestBody @Validated BizFzUserWxBindParam param) {
        return CommonResult.data(bizFzUserService.bindWxOpenId(param));
    }
    
    /**
     * 微信小程序登录
     *
     * @author xiao
     */
    @ApiOperation(value = "微信小程序登录", position = 7)
    @PostMapping("/biz/fzUser/wxLogin")
    public CommonResult<Map<String, Object>> wxLogin(@RequestBody @Validated BizFzUserWxLoginParam param) {
        Map<String, Object> result = bizFzUserService.wxLogin(param);
        if (result == null) {
            return CommonResult.error("您不具备分销用户权限，请联系管理员");
        }
        return CommonResult.data(result);
    }
    
    /**
     * 注册分销用户
     *
     * @author xiao
     */
    @ApiOperation(value = "注册分销用户", position = 8)
    @PostMapping("/biz/fzUser/register")
    public CommonResult<Map<String, Object>> register(@RequestBody @Validated BizFzUserRegisterParam param) {
        Map<String, Object> result = bizFzUserService.register(param);
        return CommonResult.data(result);
    }

    /**
     * 获取分账用户邀请码
     *
     * @author gtc
     * 
     */
    @ApiOperation(value = "获取分账用户邀请码", position = 9)
    @GetMapping("/biz/fzUser/getInviteCode")
    public CommonResult<String> getInviteCode(BizFzUserIdParam bizFzUserIdParam) {
        // 获取用户ID
        String userId = bizFzUserIdParam.getId();
        // 验证用户是否存在
        BizFzUser user = bizFzUserService.queryEntity(userId);
        if (user == null) {
            return CommonResult.error("用户不存在");
        }
        // 使用SM2加密用户ID作为邀请码
        String inviteCode = CommonCryptogramUtil.doSm2Encrypt(userId);
        return CommonResult.data(inviteCode);
    }

    /**
     * 获取全量分账用户列表
     *
     * @author gtc
     * 
     */
    @ApiOperation(value = "获取全量分账用户列表", position = 10)
    @GetMapping("/biz/fzUser/list")
    public CommonResult<List<BizFzUser>> list(BizFzUserPageParam bizFzUserPageParam) {
        // 创建查询条件
        QueryWrapper<BizFzUser> queryWrapper = new QueryWrapper<>();
        if(ObjectUtil.isNotEmpty(bizFzUserPageParam.getName())) {
            queryWrapper.lambda().like(BizFzUser::getName, bizFzUserPageParam.getName());
        }
        if(ObjectUtil.isNotEmpty(bizFzUserPageParam.getPhone())) {
            queryWrapper.lambda().like(BizFzUser::getPhone, bizFzUserPageParam.getPhone());
        }
        if(ObjectUtil.isNotEmpty(bizFzUserPageParam.getStatus())) {
            queryWrapper.lambda().eq(BizFzUser::getStatus, bizFzUserPageParam.getStatus());
        }
        if(ObjectUtil.isNotEmpty(bizFzUserPageParam.getFzRole())) {
            queryWrapper.lambda().eq(BizFzUser::getFzRole, bizFzUserPageParam.getFzRole());
        }
        if(ObjectUtil.isNotEmpty(bizFzUserPageParam.getFzType())) {
            queryWrapper.lambda().eq(BizFzUser::getFzType, bizFzUserPageParam.getFzType());
        }
        
        // 直接调用service的list方法获取全部数据
        List<BizFzUser> list = bizFzUserService.list(queryWrapper);
        return CommonResult.data(list);
    }
} 